package com.corecloud.controller.sms;

import com.corecloud.service.impl.AbstractValidateCodeProcessor;
import com.corecloud.service.sms.SmsCodeSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

@Component("smsCodeProcessor")
public class SmsCodeProcessor extends AbstractValidateCodeProcessor<SmsCode> {

    private static final String SMS_CODE_PARAM_NAME = "mobile";

    @Autowired
    private SmsCodeSender smsCodeSender;

    @Override
    protected Integer send(ServletWebRequest request, SmsCode validateCode) throws Exception {
//        取出手机号
        String mobile = ServletRequestUtils.getRequiredStringParameter(request.getRequest(), SMS_CODE_PARAM_NAME);
        String codePhoneType = ServletRequestUtils.getRequiredStringParameter(request.getRequest(), "codePhoneType");
//        取出验证码
        String code = validateCode.getCode();
//        前往发送短信
        Integer send = smsCodeSender.send(mobile, code, codePhoneType);
        return send;
    }
}
